using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace LFramework
{
    public class UIBase : MonoBehaviour
    {
        protected UICtrlBase m_UICtrl;

        private void Awake()
        {
            OnAwake();
        }

        /// <summary>
        /// 初始化组件
        /// </summary>
        protected virtual void OnAwake()
        {
        }

        /// <summary>
        /// ui初始化
        /// </summary>
        public virtual void Init()
        {
        }

        /// <summary>
        /// ui释放
        /// </summary>
        public virtual void Release()
        {
            m_UICtrl = null;
        }

        public void SetUICtrl(UICtrlBase uiCtrl)
        {
            m_UICtrl = uiCtrl;
        }

        /// <summary>
        /// 断线重接刷新接口
        /// </summary>
        public virtual void ReconnectRefresh()
        {
        }

        protected void SetIcon(Image img,string iconPath,Action<Image> loadFinishCB,bool isSetDefault = false) 
        {
            if (img == null || string.IsNullOrEmpty(iconPath))
                return ;

            Action<string, UnityEngine.Object> loadIconCB = (strResourcePath, objResource) =>
            {
                img.sprite = objResource as Sprite;
            };

            m_UICtrl.GetAsset(iconPath, loadIconCB);
        }

        #region 生成panel 相关接口

        protected T AddPanel<T>(string panelPath, Transform parent) where T : UIPanel
        {
            GameObject panelGo = m_UICtrl.AddPanel(panelPath,parent);

            if(panelGo == null)
                return null;

            T panelScript = panelGo.GetComponent<T>();
            panelScript.SetUICtrl(m_UICtrl);
            panelScript.Init();

            return panelScript;
        }

        protected void AddPanelList<T>(ref List<T> panelList , int count, string panelPath, Transform parent) where T : UIPanel 
        {
            if (panelList == null)
            {
                panelList = new List<T>(count);
            }

            while (panelList.Count < count)
            {
                T panelScripte = AddPanel<T>(panelPath, parent);

                if (panelScripte != null)
                    panelList.Add(panelScripte);
                else
                {
                    GameEntry.LogError(panelPath + "  is  Null");
                    break;
                }
            }
        }

        protected void ClearPanelList<T>(ref List<T> panelList) where T : UIPanel
        {
            if (panelList != null)
            {
                T panelScript;
                for (int i = 0; i < panelList.Count; i++)
                {
                    panelScript = panelList[i];
                    if (panelScript != null)
                    {
                        panelScript.Release();
                        m_UICtrl.RemovePanel(panelScript.gameObject);
                    }
                }
                panelList.Clear();
            }
        }

        protected void AddPanelList(ref List<GameObject> panelGoList, int count, string panelPath, Transform parent)
        {
            while (panelGoList.Count < count)
            {
                GameObject panelGo = m_UICtrl.AddPanel(panelPath, parent);

                if (panelGo != null)
                    panelGoList.Add(panelGo);
                else
                {
                    GameEntry.LogError(panelPath + "  is  Null");
                    break;
                }
            }
        }

        protected void ClearPanelList(ref List<GameObject> panelList)
        {
            if (panelList != null)
            {
                GameObject obj;
                for (int i = 0; i < panelList.Count; i++)
                {
                    obj = panelList[i];
                    if (obj != null)
                        m_UICtrl.RemovePanel(obj);
                }
                panelList.Clear();
            }
        }
        #endregion
    }
}
